// formPackage/pages/commissionManager/houseCommissionList/houseCommissionList.js

import { queryHouseCommissionList, deleteCommissionForHouse } from "../../../../service/commission_service"
import { isPushCompanyCommission } from "../../../../service/platform_service"
import {
  CHECK_SUCCESS,
  CHECK_FAIL,
  CHECK_PROGRESS
} from "../../../../service/base_service"
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    themeStyle: "",
    houseNumber: "",
    dataList: [],
    checkSuccess: CHECK_SUCCESS,
    checkFail: CHECK_FAIL,
    checkProgress: CHECK_PROGRESS,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      houseNumber: options.houseNumber,
    })

  },



  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (app.globalData.themeStyle) {
      this.setData({
        themeStyle: app.globalData.themeStyle
      })
    }
    this.queryHouseCommissionList();
  },
  //查询佣金列表:
  queryHouseCommissionList() {
    wx.showLoading();
    queryHouseCommissionList(this.data.houseNumber).then(res => {
      wx.hideLoading();
      console.log("JJLog:佣金列表是:" + JSON.stringify(res));
      if (res.code == 200) {
        this.setData({
          dataList: res.data || []
        })
      }
    }).catch(err => {
      wx.hideLoading();
      wx.showToast({
        title: err || "查询失败",
        icon: "none"
      })
      console.error("JJLog:queryHouseCommissionList", err);
    })
  },
  //跳转到这个佣金详情
  bookingAction(e) {
    let item = e.currentTarget.dataset.item;
    wx.navigateTo({
      url: '/formPackage/pages/commissionManager/addNewCommission/addNewCommission?houseNumber=' + this.data.houseNumber + "&id=" + item.id,
    })
  },
  //跳转到佣金详情:
  pushToSettingCommission() {
    wx.navigateTo({
      url: '/formPackage/pages/commissionManager/addNewCommission/addNewCommission?houseNumber=' + this.data.houseNumber,
    })
  },
  //删除一条佣金设置
  deleteOneCommission(e) {
    let id = e.currentTarget.dataset.id;
    wx.showModal({
      title: '提示',
      content: '是否删除该佣金设置？',
      confirmColor: "#FF0000",
      success: res => {
        if (res.confirm) {
          this.deleteCommissionForHouse(id);
        }
      }
    })

  },
  deleteCommissionForHouse(id) {
    wx.showLoading();
    deleteCommissionForHouse(id).then(res => {
      wx.hideLoading();
      if (res.code == 200) {
        wx.showToast({
          title: '操作成功',
          icon: "none"
        })
        this.queryHouseCommissionList();
      }
    }).catch(err => {
      wx.hideLoading();
      wx.showToast({
        title: err || "操作失败",
        icon: "none"
      })
      console.error("JJLog:deleteCommissionForHouse", err);
    })
  },

  //是否关闭或者开启房源报备状态
  upOrDownClick(e) {
    let status = e.currentTarget.dataset.ispush;
    let index = e.currentTarget.dataset.index;
    let id = e.currentTarget.dataset.id;
    let remindText = "";
    if (!status) {
      remindText = "是否开启房源报备?"
    } else {
      remindText = "是否关闭房源报备?"
    }

    wx.showModal({
      title: '提示',
      content: remindText,
      confirmColor: "#FF0000",
      success: res => {
        if (res.confirm) {
          this.settingReportStatus(status, index, id);
        }
      }
    })

  },
  settingReportStatus(status, index, id) {
    wx.showLoading();
    isPushCompanyCommission(id).then(res => {
      wx.hideLoading();
      if (res.code == 200) {
        wx.showToast({
          title: '操作成功',
          icon: "success"
        })
        if (status) {
          let item = 'dataList[' + index + '].isPush';
          this.setData({
            [item]: false,
          })
        } else {
          let item = 'dataList[' + index + '].isPush';
          this.setData({
            [item]: true,
          })
        }
      }
    }).catch(err => {
      wx.hideLoading();
      wx.showToast({
        title: err || "操作失败",
        icon: "none"
      })
    })
  },


})